<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {


	public function index()
	{
		$this->load->library('parser');
		$data['title']='Login';
		$this->parser->parse('header', $data);
		$this->load->view('login');
   		$this->load->view('footer');
	}
  
  public function check(){
    $this->load->library('encrypt');
   
    $login = $this->input->post('email');
     $password = $this->input->post('password');
    $verifycode = $this->input->post('verifycode');
    if (empty($login)){
      echo '邮箱不能为空';exit;
    }else if(empty($password)){
      echo '密码不能为空' ;exit;
    }else if (empty($verifycode)){
      echo '验证码不能为空';exit;
    }
    
 
   $sql = "SELECT * FROM sdk_user WHERE login_name = ? and password = ?";
    $rows = $this->db->query($sql, array($login,$password)); 
        
    if ($rows->num_rows > 0){

      $r = $rows->result();
     
    
        $this->session->set_userdata($r[0]);
        header("Location:/sdksystem");exit;       //c92c8ac9  
    }
  }
  
  public function logout(){
    $this->session->sess_destroy();
    header("Location:/user");exit;  
  }
  
  public function sendvc(){
    $email = $this->input->get('email');
    if (empty($email)){echo 'please enter email';exit;}
    $mode="/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/";
    if(!preg_match($mode,$email,$content)){  	echo "email fromt error"; exit;  }
    
    
    
    $query = $this->db->query('SELECT uid FROM sdk_user where login_name="' . $email . '" LIMIT 1');
    $row = $query->row();
    if ($row == null){
      echo 'email error';exit;
    }else{
      $this->send_email_vc($email);
      
    }
  }
  
  public function  send_email_vc($email){                                 
    //配置邮箱参数
    $config['protocol']= 'smtp';
    
    /*$config['smtp_host'] = 'ssl://smtp.gmail.com';  
    $config['smtp_user'] = 'souquick@gmail.com';  
    $config['smtp_pass'] = 'sdk123456';  
    $config['smtp_port'] = '465';     */
    
    $config['smtp_host'] = 'ssl://smtp.qq.com';  
    $config['smtp_user'] = '345933361@qq.com';  
    $config['smtp_pass'] = 'Zhaoyan@611';  
    $config['smtp_port'] = '465';
      
    $config['charset'] = 'utf-8';  
    $config['wordwrap'] = TRUE;  
    $config['mailtype'] = 'text'; 
    $config['newline'] = "\r\n";
    $config['crlf'] = "\r\n";  
    
    $this->load->library('email',$config); 
    
    $verifycode =  $this->randomkeys(8);
    
    $this->update_loginfo($email,$verifycode);
    
    //开始发送邮件
    $this->email->clear();

    $this->email->to($email);
    $this->email->from('root@Souquick.com');
    $this->email->subject('Your Login VerifyCode');
    $this->email->message('VerifyCode: ' . $verifycode);
    $this->email->send();
    
    //echo $this->email->print_debugger();
    echo 'VerifyCode Is Send';exit;
  }
  
  
  private function update_loginfo($email,$verifycode){
      
      $ip = $this->get_ip();
      $data = array('last_login' => time(), 'last_ip' => $ip, 'verify_code' => $verifycode);
      $where = "login_name = '" . $email . "'";
      
      
      $str = $this->db->update_string('sdk_user', $data, $where);
      $this->db->query($str);
  }
  
  private function randomkeys($length)
  {
   $pattern='1234567890abcdefghijklmnopqrstuvwxyz';
   $key = '';
   for($i=0;$i<$length;$i++)
   {
     $key .= $pattern{mt_rand(0,15)};    //生成php随机数
   }
   return $key;
  }
  
  private function get_ip()
  {
    global $ip;
    if (getenv("HTTP_CLIENT_IP"))
    $ip = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR"))
    $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("REMOTE_ADDR"))
    $ip = getenv("REMOTE_ADDR");
    else $ip = "Unknow";
    return $ip;
  }

  
}